﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using System.Net;
using System.Net.Sockets;
using System.IO;
using StressTesterCommon;
using System.Xml.Linq;
using System.ComponentModel;
using System.Xml;
using System.Xml.Xsl;


namespace StressServer
{
    public class QueryResultExtended
    {
        #region Constructors
        public QueryResultExtended(Query Query)
        {
            _Query = Query;
        }
        #endregion
        #region Members
        private Query _Query;
        public Query Query
        {
            get { return _Query; }
            set { _Query = value; }
        }
        private List<StressClientProxy> _Clients;
        public List<StressClientProxy> Clients
        {
            get { return _Clients; }
            set { _Clients = value; }
        }
        #endregion
        #region Methods

        #endregion
    }
    public class TestResult
    {
        #region Members
        private Test _Test;
        public Test Test
        {
            get { return _Test; }
            set { _Test = value; }
        }
        private List<StressClientProxy> _Clients;
        #endregion


        private void GenerateUniqueQueries()
        {
            var values = (from Client in _Clients
                          from QueryRes in Client.QueryResults
                          select QueryRes.Duration);
            double SumDuration = values.Sum();
            double AvgDuration = values.Average();
            double MaxDuration = values.Max();
            double MinDuration = values.Min();

                    
        }

        private XElement GenerateSummaryDurations()
        {
           

            var values = (from Client in _Clients
                          from QueryRes in Client.QueryResults
                          select QueryRes.Duration);
            double SumDuration = values.Sum();
            SumDuration = SumDuration / 1000;
            double AvgDuration = values.Average();
            double MaxDuration = values.Max();
            double MinDuration = values.Min();

            DateTime StartDate = (from Client in _Clients
                                  from QueryRes in Client.QueryResults
                                  select QueryRes.QueryStartTime).Min();
            DateTime EndDate = (from Client in _Clients
                                from QueryRes in Client.QueryResults
                                select QueryRes.QueryEndTime).Max();
            int NumberOfQueries = (from Client in _Clients
                                   from QueryRes in Client.QueryResults
                                   select QueryRes).Count();
            double QueriesPerSecond = NumberOfQueries / SumDuration;
            XElement Ret = new XElement("summarydurations");
            Ret.Add(new XElement("sumduration") { Value = SumDuration.ToString().Replace(',', '.') });
            Ret.Add(new XElement("avgduration") { Value = AvgDuration.ToString().Replace(',', '.') });
            Ret.Add(new XElement("maxduration") { Value = MaxDuration.ToString().Replace(',', '.') });
            Ret.Add(new XElement("minduration") { Value = MinDuration.ToString().Replace(',', '.') });
            Ret.Add(new XElement("queriespersecond") { Value = QueriesPerSecond.ToString() });
            Ret.Add(new XElement("numberofqueries") { Value = NumberOfQueries.ToString() });
            return Ret;
        }
        public void GenerateReport(string FilePath, List<StressClientProxy>Clients, Test Test, DateTime TestStarted, DateTime TestEnded)
        {
            _Clients = Clients;
            _Test = Test;
            XDocument TestResult = new XDocument();
            XElement Root = new XElement("testresult");
            Root.Add(new XElement("started") {Value=TestStarted.ToString()});
            Root.Add(new XElement("ended") { Value = TestEnded.ToString() });
            TimeSpan T = TestEnded - TestStarted;
            Root.Add(new XElement("duration") { Value = T.TotalMilliseconds.ToString().Replace(',','.') });
            TestResult.Add(Root);
            Root.Add(_Test.ToXDocument().Element("test"));
            Root.Add(GenerateSummaryDurations());
            XslCompiledTransform Trans = new XslCompiledTransform();
            Trans.Load(@"C:\Users\peter\Documents\Visual Studio 2012\Projects\StressTester\StressServer\XSLTReport.xslt");
            TestResult.Save(@"C:\Users\peter\Documents\Visual Studio 2012\Projects\StressTester\TestResults\Report.xml");
            StringBuilder Output = new StringBuilder();

            XmlWriter Writer = XmlWriter.Create(Output);
            
            Trans.Transform(TestResult.CreateReader(), Writer);
            StreamWriter Write = new StreamWriter(@"C:\Users\peter\Documents\Visual Studio 2012\Projects\StressTester\TestResults\testreport.html");
            Write.Write(Output);
                   
        }
     
    }
}
